//
//  RESTClient.m
//  doubanFun
//
//  Created by bing on 16/3/22.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "RESTClient.h"
#import <AFNetworking/AFNetworking.h>

typedef void(^NetSuccess) (NSURLSessionDataTask *, id);
typedef void(^NetFailure) (NSURLSessionDataTask *, NSError *);

@interface RESTClient ()
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end

@implementation RESTClient
+ (instancetype)sharedClient {
    static RESTClient *client;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[self alloc] init];
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURL *baseURL = [NSURL URLWithString:[NSString stringWithUTF8String:BASE_URL]];
        client.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL sessionConfiguration:config];
        NSSet *accpetSet = client.manager.responseSerializer.acceptableContentTypes;
        accpetSet = [accpetSet setByAddingObjectsFromArray:@[@"text/html"]];
        client.manager.responseSerializer.acceptableContentTypes = accpetSet;
    });
    return client;
}

- (void)method:(METHOD)method urlString:(NSString *)urlString parameters:(id)parameters
       success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    
    [_manager.requestSerializer setValue:@"Content-Type" forHTTPHeaderField:@"application/x-www-form-urlencoded"];
    
    NetSuccess  mysuccess = ^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        // code 不为 0 ,为业务逻辑错误
        NSInteger code  = [[responseObject objectForKey:@"r"] integerValue];
        if (code && failure)
        {
            NSString *error  = [responseObject objectForKey:@"err"];
            NSString *domain = dataTask.currentRequest.URL.absoluteString;
            failure([NSError errorWithDomain:domain code:code userInfo:@{@"err": error}]);
        }
        else if (success)
        {
            success(responseObject);
        }
    };
    
    NetFailure myfailure = ^(NSURLSessionDataTask *dataTask, NSError *error) {
        if (failure) {
            failure(error);
        }
    };
    
    switch (method) {
        case GET:{
            [_manager GET:urlString parameters:parameters progress:nil success:mysuccess failure:myfailure];
        }
            break;
        case POST:
            [_manager POST:urlString parameters:parameters progress:nil success:mysuccess failure:myfailure];
            break;
        case PATCH:{
            [_manager PATCH:urlString parameters:parameters success:mysuccess failure:myfailure];
        }
            break;
        case DELETE:{
            [_manager DELETE:urlString parameters:parameters success:mysuccess failure:myfailure];
        }
        case HEAD: {
            void (^headsuccess)(NSURLSessionDataTask *task) = ^(NSURLSessionDataTask *task) {
                if (failure) {
//                TODO:获取 HEAD数据
//                    failure();
                }
            };
            [_manager HEAD:urlString parameters:parameters  success:headsuccess failure:myfailure];
        }
            break;
        default:
            break;
    }
    
}
@end